package com.ibeeking.found.msg.rest.service.mpevent;

import com.ibeeking.found.msg.rest.service.IWxMpFansService;
import com.ibeeking.found.msg.service.common.bo.WxMpFansBO;
import com.ibeeking.found.msg.service.common.dos.WxMpFansDO;

import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 用户取消关注事件处理
 *
 * @Author ibeeking
 */
@Component
public class UnsubscribeHandler  implements WxMpMessageHandler {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private IWxMpFansService mpFansServiceImpl;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        WxMpFansBO wxMpFansBO = mpFansServiceImpl.queryByOpenId(wxMessage.getFromUser());
        if (null != wxMpFansBO) {
            WxMpFansDO wxMpFansDO = new WxMpFansDO();
            wxMpFansDO.setId(wxMpFansBO.getId());
            wxMpFansDO.setSubscribe(0);
            wxMpFansDO.setCancelSubscribeTime(LocalDateTime.now());
            mpFansServiceImpl.modifyInfo(wxMpFansDO);
        }
        return null;
    }
}
